package homeWorks;

import java.io.*;

/**
 * @author lijun
 * @date 2022-08-27 20:54
 * @fileName homeWork
 */
public class Test02 {
    public static void main(String[] args) throws IOException {



        /**Test03
         * 将当前目录下的所有文件都复制一份
         * 复制的文件命名为:原文件名_cp.后缀
         * 比如原文件为:test.dat
         * 复制后的文件为:test_cp.dat
         */
        File file = new File("C:/Users/lijun/Desktop/IMGs");
            File[] subs = file.listFiles( f -> f.isFile());
            for (File sub : subs) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sub));
                    StringBuilder sb = new StringBuilder( sub.getPath());
                    String newName = new String(sb.insert(sb.lastIndexOf("."), "_cp"));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newName));
                    int d ;
                    while ((d = bis.read()) != -1) {
                        bos.write(d);
                    }
                    System.out.println("已完成" + newName + "的复制");
                    bis.close();
                    bos.close();
            }







        /**Test02
         * 改正下面程序的错误
         * 程序实现需求:使用缓冲流完成文件的复制操作
         */
        /*
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test_cp.txt"));
        int d;
        while ((d = bis.read()) != -1) {
            bos.write(d);
        }
        bos.flush();//清空(强制输出)缓冲的数据
        bos.close();
        System.out.println("复制完毕!");
         */

        /**
         * Test01
         * 改正下面程序的错误
         * 程序实现的功能需求:复制一个文件
         */
       /*
        FileInputStream fis = new FileInputStream("test.txt");
        FileOutputStream fos = new FileOutputStream("test_cp.txt");

        int d;
        while ((d = fis.read()) != -1) {
            fos.write(d);
        }
        System.out.println("复制完毕!");
        fis.close();
        fos.close();*/
    }
}
